//
//  GHTabbarController.swift
//  KYC2C
//
//  Created by Guanghui Liao on 12/11/17.
//  Copyright © 2017 liaoworking. All rights reserved.
//

import UIKit

class GHTabBarController: UITabBarController {


    override func viewDidLoad() {
    super.viewDidLoad()
        //主色调  233 0 25
        configTextAttribute()
        addControllers()
        configDelegate()
    }
    
    func addControllers()  {
        addChildVC(childVC: ViewController(), title: "左边", imageName: "", selectedImageName: "")
        addChildVC(childVC: ViewController(), title: "中间", imageName: "", selectedImageName: "")
        addChildVC(childVC: ViewController(), title: "右边", imageName: "", selectedImageName: "")

    }
    
    //tabbarItem颜色
    func configTextAttribute() {
        // 通过 appearance统一设置UITabBarItem的文字属性，属性后面带UI_APPEARANCE_SELECTOR的方法, 都可以通过appearance对象来统一设置
        let tabBar = UITabBarItem.appearance()
        let attrs_Normal = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 12), NSAttributedStringKey.foregroundColor: UIColor.gray]
        let attrs_Select = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 12), NSAttributedStringKey.foregroundColor: AppMainColor]
        tabBar.setTitleTextAttributes(attrs_Normal, for: .normal)
        tabBar.setTitleTextAttributes(attrs_Select, for: .selected)
    }
    
    func addChildVC(childVC:UIViewController, title:String, imageName:String, selectedImageName:String) {
        
        childVC.title = title
        childVC.tabBarItem.image = UIImage(named: imageName)
        childVC.tabBarItem.selectedImage = UIImage(named: selectedImageName)
        
        let nav = GHNavVC(rootViewController: childVC)
        
        addChildViewController(nav)
    }
    
}

extension GHTabBarController: UITabBarControllerDelegate {
    
    func configDelegate() {
        self.delegate = self
    }
    
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        let index = tabBarController.viewControllers?.indexes(of: viewController).first as! Int
        
        
//        if (index == 2) {
//
//            
//            //这个不能写全局的东西  不然就一直不释放 
//            if BaseService().userInfoModel == nil {
//
//                SGlobalCenter?.login({ (hasLogin) in
//
//                })
//                return false
//            }
//        }
        return true
    }
    
    
}
